﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JieBooLaw.Common.Constant;

namespace JieBooLaw.Common.Web
{
    public class AdminHttpModule : IHttpModule
    {
        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            context.EndRequest += new EventHandler(context_EndRequest);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_EndRequest(object sender, EventArgs e)
        {

        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;
            HttpResponse response = context.Response;
            HttpRequest request = context.Request;

            if (request.Url.AbsoluteUri.EndsWith(".aspx") || request.Url.AbsoluteUri.EndsWith(".ashx"))
            {
                if (context.Session == null || context.Session[StringConstant.LoginUserInfo] == null)
                {
                    if (!request.Url.AbsoluteUri.EndsWith("Login.aspx"))
                    {
                        response.Redirect("~/Login.aspx");
                    }
                }
            }
        }

        #endregion

        #region IHttpModule Members

        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}
